package com.dashu.device.service;

import com.dashu.base.enums.common.Status;
import com.dashu.base.enums.device.Iden;
import com.dashu.base.vo.DevicePicDelReqVO;
import com.dashu.device.model.CompanyTeacher;
import com.dashu.device.model.QCompanyTeacher;
import com.querydsl.core.BooleanBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.FlushModeType;
import javax.transaction.Transactional;


@Slf4j
@Service
public class CompanyTeacherService extends DataService {


    @Autowired
    private DeviceService deviceService;


    @Transactional
    public CompanyTeacher findTeacher(long companyTeacherId) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.id.eq(companyTeacherId));
        CompanyTeacher companyTeacher =
                queryFactory
                        .select(qCompanyTeacher)
                        .from(qCompanyTeacher)
                        .where(booleanBuilder.getValue())
                        .fetchOne();
        return companyTeacher;

    }


    @Transactional
    public void upTeacherStatus(long companyTeacherId) {
        QCompanyTeacher qCompanyTeacher = QCompanyTeacher.companyTeacher;
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(qCompanyTeacher.id.eq(companyTeacherId));
        CompanyTeacher companyTeacher = queryFactory
                .select(qCompanyTeacher)
                .from(qCompanyTeacher)
                .where(booleanBuilder.getValue())
                .fetchOne();
        companyTeacher.setStatus(Status.REVIEWED);
        entityManager.merge(companyTeacher);
        entityManager.setFlushMode(FlushModeType.AUTO);
    }

}

